package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/6-18:50
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    public Result add(@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.addBatch(checkGroup);
            return Result.OK(MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return Result.ERROR(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());

        return pageResult;

    }
    @RequestMapping("/findById")
    public Result findById(@RequestParam Integer id){
        //将需要的三个数据封装到map集合中
        Map map= checkGroupService.findById(id);
        return Result.OK("回显数据成功",map);
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result edit(@RequestBody CheckGroup checkGroup){
        try{

            checkGroupService.edit(checkGroup);
            return Result.OK(MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            return Result.ERROR(MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delete(@RequestParam Integer id){
        try{
            checkGroupService.delete(id);
            return Result.OK(MessageConstant.DELETE_CHECKGROUP_SUCCESS);

        } catch (Exception e){
            return Result.ERROR(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return Result.OK("",checkGroups);
    }
}
